package com.info.zhiduoduo.common.utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * @author tianyonghong
 * @create 2022/2/18
 */
@Slf4j
public class ConvertPdfFile {

    // 服务器安装插件/字体
    //yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    //yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
    //参照服务器环境配置
    private static final String chromePath = "google-chrome";//正式使用
    private static final String chromePath_local = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome";//mac测试使用

    private static final String shell = "%s --headless --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --print-to-pdf-no-header --print-to-pdf='%s' '%s' ";

    /**
     * html生成pdf
     * @param url   html完整文件路径及文件名称,eg:/usr/test.html
     * @param outPutFilePath 输出文件路径
     * @param outPutFileName 输出文件名称
     * @return
     */
    @SneakyThrows
    public static File convert(String url,String outPutFilePath,String outPutFileName){
        Process exeEcho = null ;
        //生成sh临时文件
        File shellFile = File.createTempFile(UUID.randomUUID().toString(),".sh");
        //生成pdf文件
        File pdfFile = new File(outPutFilePath,outPutFileName);
        FileOutputStream outputStream;
        try {
            outputStream = new FileOutputStream(shellFile);
            String shellStr = "";
            if(ContextUtil.isLocal()) {
                shellStr = String.format(shell, chromePath_local, pdfFile.getPath(), url, "utf8");
            }else {
                shellStr = String.format(shell, chromePath, pdfFile.getPath(), url, "utf8");
            }
            outputStream.write(shellStr.getBytes());
            outputStream.close();

            exeEcho = Runtime.getRuntime().exec("sh "+shellFile.getPath());
            exeEcho.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成文件失败,Error:{}",e.getMessage());
        } finally {
            // 关闭chrome进程
            if(exeEcho != null){
                exeEcho.destroy();
            }
        }
        //删除临时文件
        shellFile.delete();
        return pdfFile;
    }

//    public static void main(String[] args) throws Exception{
//        HashMap<String, Object> map = initData();
//        //使用freemarker 生成html
//        WordUtil.createHtmlToPdf(map, "zdd_delivery_template.ftl","Users/tianyonghong/Desktop", "zddDelivery.pdf");
//    }
//
//    public static HashMap<String, Object> initData() {
//        HashMap<String, Object> needReplaceMapData = new HashMap<String, Object>();
//        /******************************************单个属性************************************/
//        needReplaceMapData.put("publishTime", "2021年12月31日");
//        needReplaceMapData.put("compname", "企业名称");
//        needReplaceMapData.put("username", "天津市南开区陈宝创客软件开发工作室");
//        needReplaceMapData.put("projectname", "测试余额判断");
//        needReplaceMapData.put("projectstart", "2021年12月30日");
//        needReplaceMapData.put("projectfinish", "2021年12月31日");
//        needReplaceMapData.put("projectAmount", "20,000");
//        needReplaceMapData.put("moneyChinese", "贰万元");
//
//        /******************************************list 属性添加***********************************/
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 2; i++) {
//            list.add("测试看看" + i);
//        }
//        needReplaceMapData.put("list", list);
//
//        /****************************************** 图片 ***********************************/
//        File compsealFile = new File("/Users/tianyonghong/Desktop", "xinxi.png");
//        needReplaceMapData.put("compseal", ImgBase64Util.ImgToBase64(compsealFile.getAbsolutePath()));
//        return needReplaceMapData;
//    }
}
